<!--
 * @Author: 白川 杏 baiyufeisteam@163.com
 * @Date: 2023-04-19 08:23:23
 * @LastEditors: 白川 杏 baiyufeisteam@163.com
 * @LastEditTime: 2023-06-11 09:58:03
 * @FilePath: \backend\vueproject\src\components\User.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <div>
            <el-divider content-position="left">您具有一定的操作权限</el-divider>
        </div>

        <div>
            <el-divider>
                <span id="spanlittlepart">查询</span>
            </el-divider>
        </div>

        <!-- 查询 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="花盆名称">
                <el-input v-model="checkformInline.potname" placeholder="potname"></el-input>
            </el-form-item>
            <el-form-item style="width: 200px;">
                <el-button @click="search" type="primary" plain style="width: 200px;">查询</el-button>
            </el-form-item>
        </el-form>

        <el-divider>
            <el-icon><star-filled /></el-icon>
        </el-divider>

        <!-- 展示表格 -->
        <el-table :data="tableData" style="float: left;" :highlight-current-row="true" ref="table" width="100%" >
            <el-table-column label="花盆图片" width="" class="table_column">
                <template #default="scope">
                    <div style="display: flex;">
                        <el-image
                            style="width: 100px; height: 100px; padding-left: -30px;margin-right: 10px;margin-left: -30px; overflow: visible;"
                            :src="baseURL + tableData[scope.$index].potimg"
                            :preview-src-list="[baseURL + tableData[scope.$index].potimg]" :preview-teleported="true"
                            :initial-index="scope.$index" />
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="potname" label="花盆名称" width="" />
            <el-table-column label="操作" width="">
                <template #default="scope">
                    <el-button link type="primary" size="small"
                        @click.prevent="handleEdit(scope.$index, scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div style="position: relative;left: -80px;top:30px;">分页：</div>

        <el-pagination background layout="prev, pager, next" :current-page="page" @update:current-page="handleCurrentChange"
            :total="total" :page-size="size" style="bottom: 0; text-align: center;margin-left: 45%;margin-top: 5px;" />

        <!-- Form -->
        <el-dialog v-model="dialogFormVisible" title="编辑">
            <span>请选择图片：</span>
            <el-button type="primary">
                <input type="file" value="" id="file" />
            </el-button>
            <div class="el-upload__tip">只能上传图片文件；</div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="onSaveInfo">
                        提交
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import {
    Guide,
    PictureFilled,
    User,
    HomeFilled,
    ChatLineSquare,
    Apple,
    Document,
    List,
    Tools,
    ShoppingCart,
    CollectionTag,
    Finished,
    InfoFilled,
    Cherry,
    Goods,
    Sell,
    ShoppingCartFull,
    Van,
    Sunrise,
    StarFilled,
    EditPen,
    Edit,
    Promotion,
    Grape,
    Pear,
} from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';
const formLabelWidth = '140px';
</script>
<script>

export default {
    data() {
        return {
            baseURL: 'http://localhost:8080',
            formInline: {
                potid: 0,
                potimg: '',
                potname: ''
            },
            checkformInline: {
                potid: 0,
                potimg: '',
                potname: ''
            },
            dialogFormVisible: false,
            showSearch: false,
            tableData: [],
            tableDataStore: [],
            tableimgList: [],
            tableimg: '',
            bg: {
                potid: 0,
                potimg: '',
                potname: ''
            },
            basedata: '',
            form: {
                potid: 0,
                potimg: '',
                potname: ''
            },
            searchForm: [],
            filetype: [
                "jpg",
                "jpeg",
                "png",
            ],
            page: 1, // 当前页码
            total: 0, // 总条数
            size: 10, // 每页的数据条数
            reallyTable: [],
            selData: [],
            editfileList: [],
            fileArray: [],
        }
    },
    methods: {
        search() {
            let arr = []
            console.log(this.tableDataStore.length)
            for (let i = 0; i < this.tableDataStore.length; i++) {
                if (String(this.tableDataStore[i].potname).includes(this.checkformInline.potname)) {
                    arr.push(this.tableDataStore[i])
                }
            }
            console.log(arr);
            this.tableData = arr.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.selData = arr;
            this.total = arr.length;
        },
        filterArr(arr1, arr2) {
            var uid1 = []
            var uid2 = []
            for (var i in arr1) {
                uid1.push(arr1[i])
            }
            for (var j in arr2) {
                uid2.push(arr2[j])
            }
            const arr = [...uid1, ...uid2];
            console.log(arr);
            const newArr = arr.filter(item => {
                return !(uid2.includes(item));
            });
            return newArr;
        },
        //编辑表格行
        handleEdit(index, row) {
            console.log(index, row);
            let dif = this.filterArr([1, 2, 3], [2, 3, 4])
            console.log(dif);
            this.editfileList.push({ name: '生长图片', url: "http://localhost:8080" + row.growimg })
            this.dialogFormVisible = true;
            axios({
                method: 'post',
                url: 'http://localhost:8080/flowerpot/getsingle',
                withCredentials: true,
                data: {
                    potid: index
                },
            }).then(res => {
                this.form = row;
            })
        },
        async onSaveInfo() {
            if (this.form.potname != '') {
                const fileArray = this.editfileList;
                console.log(this.editfileList);
                this.fileArray = fileArray
                console.log(fileArray);
                for (let i = fileArray.length - 1; i >= 0; i--) {
                    let lastName = this.getLastName(fileArray, i)
                    await this.getBase64(fileArray[i].url).then(res => {
                        this.fileArray[i].url = res
                        this.fileArray[i].name = new Date().getTime() + Math.floor(Math.random() * (0 - 1000)) + 1000 + '.' + lastName
                    })
                }

                axios({
                    method: 'post',
                    url: 'http://localhost:8080/flowerpot/modifyuuploadflowerpotimgbackend',
                    withCredentials: true,
                    data: {
                        potid: this.form.potid,
                        potname: this.form.potname,
                        potimg: this.fileArray
                    },
                }).then(res => {
                    this.tableData = res.data.data;
                    this.tableDataStore = res.data.data;
                    console.log(res.data.data);
                    this.tableData = res.data.data.slice(
                        (this.page - 1) * this.size,
                        this.page * this.size
                    );
                    this.total = res.data.data.length;
                    this.selData = res.data.data;
                    for (var i = 0; i < res.data.data.length; i++) {
                        this.tableimgList.push(res.data.data[i].potimg);
                    }
                    //console.log("tableimglist:" + this.tableimgList);
                    this.tableimg = this.tableimgList[0];
                    this.dialogFormVisible = false;
                })
            } else {
                ElMessage.error('存在未输入项')
            }
        },
        async handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.page = val
            this.tableData = this.selData.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            // this.currentPage = val;
            const data = { 'page': val }
            // await this.onload(data)
        },
    },
    mounted() {
        //页面加载完成后，将数据库内的内容赋给定义的data变量
        axios({
            method: 'post',
            url: 'http://localhost:8080/flowerpot/getpot',
            withCredentials: true,
        }).then(res => {
            this.$nextTick(() => {
                this.$refs.table.doLayout()
            })
            this.tableData = res.data.data;
            this.tableDataStore = res.data.data;
            console.log(res.data.data);
            this.tableData = res.data.data.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.total = res.data.data.length;
            this.selData = res.data.data;
            for (var i = 0; i < res.data.data.length; i++) {
                this.tableimgList.push(res.data.data[i].potimg);
            }
            //console.log("tableimglist:" + this.tableimgList);
            this.tableimg = this.tableimgList[0];
            //console.log("tableimg：-------->" + this.tableimg);

        })
    }
}
</script>
<style scoped>
#sidepart {
    width: 100%;
    position: absolute;
    top: 5px;
    font-family: '华文中宋';
}

#mainpart {
    width: 100%;
    position: absolute;
    top: 20px;
    /* left: 260px; */
    font-family: '华文中宋';
}

/* dialog */
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

#spanpart {
    font-size: 30px;
    font-family: '华文中宋';
}

#spanlittlepart {
    font-size: 15px;
    color: cadetblue;
    font-family: '华文中宋';
}

.spaninput {
    margin-right: 10px;
    margin-top: 10px;
}

.table_column{
    display: table-cell !important;
}
</style>